# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: leon

from coin.exchange.base.kr_rest.currency import NativeCurrency
from coin.exchange.base.kr_rest.currency_constants import (
    BCHABC_BCHSV_FORK_TIME,
    BCHA_BCHN_FORK_TIME)
from coin.exchange.huobi.kr_rest.constants import currency_list


def validate_spot_native_currency(currency):
  assert type(currency) == str, str(currency)
  assert currency.lower() == currency
  assert currency.upper() in currency_list, currency


def validate_futures_native_currency(currency):
  assert type(currency) == str, str(currency)
  assert currency.upper() == currency, currency
  assert currency.upper() in currency_list, currency


def to_native_currency(currency_str):
  if currency_str == 'BCHSV':
    native_currency_str = 'bsv'
  elif currency_str in ('BCHABC', 'BCHN'):
    native_currency_str = 'bch'
  else:
    native_currency_str = currency_str.lower()
  validate_spot_native_currency(native_currency_str)
  return native_currency_str


def from_native_currency(native_currency_str, current_datetime):
  validate_spot_native_currency(native_currency_str)
  if native_currency_str == 'bsv':
    assert current_datetime >= BCHABC_BCHSV_FORK_TIME, current_datetime
    currency_str = 'BCHSV'
  elif native_currency_str == 'bch':
    if current_datetime < BCHABC_BCHSV_FORK_TIME:
      currency_str = 'BCH'
    elif BCHABC_BCHSV_FORK_TIME <= current_datetime < BCHA_BCHN_FORK_TIME:
      currency_str = 'BCHABC'
    else:
      currency_str = 'BCHN'
  elif native_currency_str == 'bcha':
    assert BCHA_BCHN_FORK_TIME <= current_datetime
    currency_str = 'BCHA'
  else:
    currency_str = native_currency_str.upper()
  return currency_str


def to_native_currency_futures(currency_str):
  if currency_str == 'BCHSV':
    native_currency_str = 'BSV'
  elif currency_str in ('BCHABC', 'BCHN'):
    native_currency_str = 'BCH'
  else:
    native_currency_str = currency_str.upper()
  validate_futures_native_currency(native_currency_str)
  return native_currency_str


def from_native_currency_futures(native_currency_str, current_datetime):
  validate_futures_native_currency(native_currency_str)
  if native_currency_str == 'BSV':
    assert current_datetime >= BCHABC_BCHSV_FORK_TIME, current_datetime
    currency_str = 'BCHSV'
  elif native_currency_str == 'BCH':
    if current_datetime < BCHABC_BCHSV_FORK_TIME:
      currency_str = 'BCH'
    elif BCHABC_BCHSV_FORK_TIME <= current_datetime < BCHA_BCHN_FORK_TIME:
      currency_str = 'BCHABC'
    else:
      currency_str = 'BCHN'
  elif native_currency_str == 'BCHA':
    assert BCHA_BCHN_FORK_TIME <= current_datetime
    currency_str = 'BCHA'
  else:
    currency_str = native_currency_str.upper()
  return currency_str


class HuobiCurrency(NativeCurrency):
  _exchange = "Huobi"

  def __init__(self, currency):
    assert currency.upper() == currency, currency
    self.native_currency_spot = to_native_currency(currency)
    self.native_currency_futures = to_native_currency_futures(currency)
    super().__init__(currency, self.native_currency_spot)

  @classmethod
  def FromStrCurrencyImpl(cls, currency_str, current_datetime):
    return HuobiCurrency(currency_str)

  @classmethod
  def FromStrNativeCurrencyImpl(cls, native_currency_str, current_datetime):
    try:
      return HuobiCurrency(from_native_currency(native_currency_str, current_datetime))
    except (AssertionError, ValueError):
      pass
    try:
      return HuobiCurrency(from_native_currency_futures(
          native_currency_str, current_datetime))
    except (AssertionError, ValueError):
      pass
    raise ValueError(native_currency_str)
